package com.example.myapplication.function.musicmode

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

// 波形显示视图
class WaveFormView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    
    private val amplitudes = mutableListOf<Float>()
    private val maxAmplitudes = 100
    
    private val paint = Paint().apply {
        color = Color.BLUE
        strokeWidth = 3f
        isAntiAlias = true
    }
    
    fun updateAmplitude(amplitude: Float) {
        amplitudes.add(amplitude)
        if (amplitudes.size > maxAmplitudes) {
            amplitudes.removeAt(0)
        }
        invalidate()
    }
    
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        
        if (amplitudes.isEmpty()) return
        
        val width = width.toFloat()
        val height = height.toFloat()
        val columnWidth = width / maxAmplitudes
        
        for (i in amplitudes.indices) {
            val amplitude = amplitudes[i]
            val left = i * columnWidth
            val top = height / 2 - (amplitude * height / 2)
            val right = left + columnWidth
            val bottom = height / 2 + (amplitude * height / 2)
            
            canvas.drawRect(left, top, right, bottom, paint)
        }
    }

    fun clear() {
        amplitudes.clear()
        invalidate()
    }
}

// 钢琴卷帘视图
class PianoRollView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    
    private val notes = mutableListOf<Pair<String, Double>>()
    private val maxNotes = 50
    
    private val notePaint = Paint().apply {
        isAntiAlias = true
        textSize = 24f
    }
    
    fun addNote(note: String, confidence: Double) {
        notes.add(Pair(note, confidence))
        if (notes.size > maxNotes) {
            notes.removeAt(0)
        }
        invalidate()
    }
    
    fun clear() {
        notes.clear()
        invalidate()
    }
    
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        
        if (notes.isEmpty()) return
        
        val width = width.toFloat()
        val height = height.toFloat()
        val noteHeight = height / 12 // 12个半音
        
        // 绘制钢琴键盘背景
        drawPianoBackground(canvas, width, height, noteHeight)
        
        // 绘制检测到的音符
        drawDetectedNotes(canvas, width, height, noteHeight)
    }
    
    private fun drawPianoBackground(canvas: Canvas, width: Float, height: Float, noteHeight: Float) {
        val whiteNotes = listOf("C", "D", "E", "F", "G", "A", "B")
        
        for (i in 0 until 12) {
            val noteName = ScaleTheorySystem.NOTE_NAMES[i]
            val isWhiteKey = noteName !in listOf("C#", "D#", "F#", "G#", "A#")
            
            val top = i * noteHeight
            val bottom = top + noteHeight
            
            val paint = Paint().apply {
                color = if (isWhiteKey) Color.WHITE else Color.BLACK
                strokeWidth = 1f
            }
            
            canvas.drawRect(0f, top, width, bottom, paint)
            
            // 绘制边框
            canvas.drawLine(0f, top, width, top, Paint().apply {
                color = Color.GRAY
                strokeWidth = 1f
            })
        }
    }
    
    private fun drawDetectedNotes(canvas: Canvas, width: Float, height: Float, noteHeight: Float) {
        val noteWidth = width / maxNotes
        
        notes.forEachIndexed { index, (note, confidence) ->
            val noteIndex = ScaleTheorySystem.NOTE_NAMES.indexOf(note)
            if (noteIndex != -1) {
                val left = index * noteWidth
                val top = noteIndex * noteHeight
                val right = left + noteWidth
                val bottom = top + noteHeight
                
                val color = when {
                    confidence > 0.8 -> Color.argb(200, 0, 255, 0)   // 绿色，高置信度
                    confidence > 0.6 -> Color.argb(200, 255, 255, 0) // 黄色，中等置信度
                    else -> Color.argb(200, 255, 0, 0)               // 红色，低置信度
                }
                
                canvas.drawRect(left, top, right, bottom, Paint().apply {
                    this.color = color
                })
            }
        }
    }
}